package com.hkrt.demo.presentation.screen.login

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.OnLifecycleEvent
import com.hkrt.demo.App
import com.hkrt.demo.R
import com.hkrt.demo.domain.exception.ApiException
import com.hkrt.demo.domain.exception.NoNetworkException
import com.hkrt.demo.domain.interactor.LoginInteractor
import com.hkrt.demo.presentation.base.BasePresenter
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

/**
 * @author  陈金广
 * @date 2018/4/4.
 */
class LoginPresenter @Inject constructor(private val loginInteractor: LoginInteractor) :
        BasePresenter<LoginContract.View>(), LoginContract.Presenter {

    @OnLifecycleEvent(value = Lifecycle.Event.ON_START)
    fun onStart() {
    }


    override fun login(phoneValue: String, md5pwd: String, time: String, operatorValue: String, platform: String, appVersion: String, mac: String) {
        var paramList = HashMap<String, String>()
        paramList.put("phone", phoneValue)
        paramList.put("pwd", md5pwd)
        paramList.put("time", time)
        paramList.put("operatorNo", operatorValue)
        paramList.put("platform", platform)
        paramList.put("appVersion", appVersion)
        paramList.put("mac", mac)
        loginInteractor.login(paramList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(view.getRxLifeCycle())
                .subscribe({
                    view.hideProgressDialog()
                    view.onLogined(it)
                }, {
                    it.printStackTrace()
                    view.hideProgressDialog()
                    if (it is NoNetworkException) {
                        view.showErrorMsg(App.instance.getResources().getString(R.string.emptyview_error_connection_not_found))
                    } else if (it is ApiException) {
                        view.showErrorMsg(it.message)
                    } else {
                        view.showErrorMsg(it.message)
                    }
                })
    }

}